package com.crud.web.controller;

import com.crud.config.ResultVo;
import com.crud.entity.User;
import com.crud.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author hyh
 * @date 2023/06/28 18:46:35
 */
@RestController
public class UserController {
  @Autowired
    private UserService userService;

    @PostMapping("user/save")
    public ResultVo save(@RequestBody User user) {
        userService.save(user);
        return ResultVo.success("save.ok");
    }

    @DeleteMapping("user/delete/{userId}")
    public ResultVo delete(@PathVariable Integer userId) {
        int i = userService.delete(userId);
        return i > 0 ? ResultVo.success("delete.ok") : ResultVo.failure("delete.no ok");
    }

    @GetMapping("user")
    public ResultVo findAll() {
        return ResultVo.success(userService.findAll());
    }

    @PutMapping("user")
    public ResultVo update(@RequestBody User user) {
        int rows = userService.update(user);
        return rows > 0 ? ResultVo.success("修改成功") : ResultVo.failure("修改失败");
    }
}
